package com.baozun.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.NumberFormat;

/**
 * 
 *@Title:
 *@Company:宝尊电子商务有限公司
 *@Description:
 *@Author:xiaozhou.zhou
 *@Since:2015年1月13日
 *@Copyright:Copyright (c) 2014
 *@ModifyDate:
 *@Version:1.1.0
 */
public class WebUtil {
	private static String encodeStr(String str) {
		if ( null != str ) {
			// DB3
//			str = StringHelper.replace(str, "\n", "﹩﹛﹨n﹜");
//			str = StringHelper.replace(str, "\r", "");
//			str = StringHelper.replace(str, "<", "﹩﹛lt﹜");
//			str = StringHelper.replace(str, ">", "﹩﹛gt﹜");
//			str = StringHelper.replace(str, "(", "﹩﹛lb﹜");
//			str = StringHelper.replace(str, ")", "﹩﹛rb﹜");
//			str = StringHelper.replace(str, "\"", "﹩﹛quot﹜");
//			str = StringHelper.replace(str, ".", "﹩﹛dot﹜");
//			str = StringHelper.replace(str, ",", "﹩﹛comma﹜");
//			str = StringHelper.replace(str, "'", "﹩﹛prime﹜");
//			str = StringHelper.replace(str, "&", "﹩﹛and﹜");
//			str = StringHelper.replace(str, " ", "﹩﹛blank﹜");
		}
		return str;
	}
	
	private static String decodeStr(String str) {
		if ( null != str ) {
			// DB3
//			str = StringHelper.replace(str, "﹩﹛﹨n﹜", "\n");
//			str = StringHelper.replace(str, "﹩﹛lt﹜", "<");
//			str = StringHelper.replace(str, "﹩﹛gt﹜", ">");
//			str = StringHelper.replace(str, "﹩﹛lb﹜", "(");
//			str = StringHelper.replace(str, "﹩﹛rb﹜", ")");
//			str = StringHelper.replace(str, "﹩﹛quot﹜", "\"");
//			str = StringHelper.replace(str, "﹩﹛dot﹜", ".");
//			str = StringHelper.replace(str, "﹩﹛comma﹜", ",");
//			str = StringHelper.replace(str, "﹩﹛prime﹜", "'");
//			str = StringHelper.replace(str, "﹩﹛and﹜", "&");
//			str = StringHelper.replace(str, "﹩﹛blank﹜", " ");
		}
		return str;
	}
	
	/**
	 * 将URL中字符串按某种字符集编码
	 * @param str
	 * @return
	 */
	public static String encodeURL(String str) {
		try {
			str = encodeStr(str);
			str = URLEncoder.encode(str, "UTF-8");
			return str;
		} catch ( UnsupportedEncodingException e ) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 将URL中字符串按某种字符集解码
	 * @param str
	 * @return
	 */
	public static String decodeURL(String str) {
		try {
			str = decodeStr(str);
			return URLDecoder.decode(str, "UTF-8");
		} catch ( UnsupportedEncodingException e ) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 用于编码页面传递内容
	 * @param str
	 * @return
	 */
	public static String encodeString(String str) {
		return encodeStr(str);
	}
	
	/**
	 * 用于解码页面传递内容
	 * @param str
	 * @return
	 */
	public static String decodeString(String str) {
		return decodeStr(str);
	}
	
	
	/**
	 * 生成简单的HTML select页面对象中的options
	 * @param optionValues options的value集
	 * @param defaultValue 默认值
	 * @return
	 */
	public static String genSimpleHtmlSelectOptions(String[] optionValues, String defaultValue) {
		StringBuffer sb = new StringBuffer();
		for ( String value : optionValues ) {
			sb.append("<option value=\"" + value + "\"" + (value.equals(defaultValue) ? " selected" : "") + ">" + value + "</option>");
		}
		return sb.toString();
	}
	
	
	/**
	 * 获取默认金额数字显示格式
	 * @return
	 */
	public static NumberFormat getDefaultCurrencyNumberFormat() {
		NumberFormat nfCurrency = NumberFormat.getInstance();
		nfCurrency.setMaximumFractionDigits(2);
		nfCurrency.setMinimumFractionDigits(2);
		nfCurrency.setGroupingUsed(true);	
		return nfCurrency;
	}

    public static String encodeString(String content, String code) {
        // TODO Auto-generated method stub
        try {
            return URLDecoder.decode(content, code);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
